package com.ysy.tz.mfg.component;

import com.google.common.eventbus.Subscribe;
import com.ysy.tz.core.engine.ApplicationContext;
import com.ysy.tz.core.event.LoginEvent;
import com.ysy.tz.core.utils.SQLUtils;
import com.ysy.tz.core.utils.SessionUtils;
import com.ysy.tz.mfg.dao.MenuDao;
import com.ysy.tz.mfg.entity.FlightsEntity;
import com.ysy.tz.mfg.entity.MenuEntity;
import com.ysy.tz.mfg.entity.UserEntity;
import java.util.ArrayList;
import java.util.Optional;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Control;
import javafx.scene.control.Label;
import javafx.scene.control.Pagination;
import javafx.scene.layout.*;
import javafx.stage.Screen;
import org.apache.commons.lang3.StringUtils;

public class MenuPage extends BorderPane {
    private Pagination pagination = new Pagination();
    private int rowIndex = 6;
    private int columnIndex = 6;

    public MenuPage() {
        this.pagination.getStyleClass().add("bullet");
        this.pagination.setStyle("-fx-font-size: 20;-fx-page-information-visible:false;");
        this.setCenter(this.pagination);
    }

    @Subscribe
    private void loginSuccess(LoginEvent loginEvent) {
        if (loginEvent.isSuccess()) {
            UserEntity currentUser = (UserEntity)SessionUtils.getAttribute(SessionUtils.CURRENT_USER);
            ArrayList<FlightsEntity> flightsEntities = (ArrayList)SessionUtils.getAttribute(SessionUtils.FLIGHTS);
            Optional<FlightsEntity> flight = flightsEntities.stream().filter((flightsEntity) -> {
                return flightsEntity.getFlightsCode().equals(currentUser.getFlightsCode());
            }).findFirst();
            FuncBanner funcBanner = (FuncBanner)ApplicationContext.mainPage.getTopBanner();
            funcBanner.setFlightsText(flight.get().getFlightsName());
            funcBanner.setCurrentUserLabel(currentUser.getUserName());
            if (!ApplicationContext.onlyShowGrading) {
                MenuEntity menuEntity = new MenuEntity();
                menuEntity.setEnterpriseId(currentUser.getEnterpriseId());
                menuEntity.setUserId(currentUser.getUserId());
                currentUser.setFlightsName(flight.get().getFlightsName());
                ArrayList<MenuEntity> menuEntityArrayList = (ArrayList)SQLUtils.executeSQLWithReturn(MenuDao.class, (menuDao) -> {
                    try {
                        return menuDao.searchMenuWithAuthority(menuEntity);
                    } catch (Exception var3) {
                        var3.printStackTrace();
                        return null;
                    }
                });
                int pageCount = (int)Math.ceil((double)menuEntityArrayList.size() / ((double)(this.rowIndex * this.columnIndex) * 1.0));
                ArrayList<GridPane> gridPanes = new ArrayList();

                for(int i = 0; i < pageCount; ++i) {
                    GridPane pane = new GridPane() {
                        protected void layoutChildren() {
                            super.layoutChildren();
                        }
                    };
                    pane.setAlignment(Pos.CENTER);
                    gridPanes.add(pane);
                }

                for(int i = 0; i < this.columnIndex * this.rowIndex * pageCount; ++i) {
                    Region node;
                    if (i < menuEntityArrayList.size()) {
                        MenuEntity curMenuEntity = (MenuEntity)menuEntityArrayList.get(i);
                        MenuButton menuButton = new MenuButton(curMenuEntity.getMenuName(), curMenuEntity.getRemark(), curMenuEntity.getImageName());
                        menuButton.setMenuViewName(curMenuEntity.getMenuView());
                        node = menuButton;
                    } else {
                        node = new Label();
                    }

                    VBox vBox = new VBox();
                    vBox.getChildren().add(node);
                    vBox.setPadding(new Insets(5.0));
                    VBox.setVgrow(node, Priority.ALWAYS);
                    node.prefWidthProperty().bind(vBox.widthProperty());
                    node.prefHeightProperty().bind(vBox.heightProperty());
                    int paneIndex = (int)Math.ceil((double)(i / (this.rowIndex * this.columnIndex)));
                    ((GridPane)gridPanes.get(paneIndex)).add(vBox, i % this.columnIndex, (int)Math.ceil((double)((i - paneIndex * this.rowIndex * this.columnIndex) / this.columnIndex)));
                    GridPane.setHgrow(vBox, Priority.ALWAYS);
                    GridPane.setVgrow(vBox, Priority.ALWAYS);
                }

                this.pagination.setMaxPageIndicatorCount(pageCount);
                this.pagination.setPageFactory((pageIndex) -> {
                    return (Node)gridPanes.get(pageIndex);
                });
            } else {
                String funcPermissions = currentUser.getFuncPermissions();
                String[] funcPerms = StringUtils.isNotBlank(funcPermissions) ? funcPermissions.split(",") : new String[0];
                ArrayList<MenuEntity> menuEntityArrayList = new ArrayList();

                for(int i = 0; i < funcPerms.length; ++i) {
                    switch (funcPerms[i]) {
                        case "DJ001":
                            MenuEntity m1 = new MenuEntity();
                            m1.setMenuCode("DJ001");
                            m1.setMenuName("用户管理");
                            m1.setImageName("man.png");
                            m1.setMenuView("sys_User");
                            m1.setRemark("用户的增删改查及功能权限");
                            menuEntityArrayList.add(m1);
                            break;
                        case "DJ003":
                            new MenuEntity();
                            MenuEntity m3 = new MenuEntity();
                            m3.setMenuCode("DJ003");
                            m3.setMenuName("毛菜称重");
                            m3.setImageName("baitiaodingji.png");
                            m3.setMenuView("sys_CarcassOutWarehouse");
                            m3.setRemark("接收的毛菜重量的称重");
                            menuEntityArrayList.add(m3);
                            break;
                        case "DJ004":
                            new MenuEntity();
                            MenuEntity m4 = new MenuEntity();
                            m4.setMenuCode("DJ004");
                            m4.setMenuName("面称重");
                            m4.setImageName("baitiaochaxun.png");
                            m4.setMenuView("sys_PigGradingList");
                            m4.setRemark("接收的面重量的称重");
                            menuEntityArrayList.add(m4);
                            break;
                        case "DJ005":
                            new MenuEntity();
                            MenuEntity m5 = new MenuEntity();
                            m5.setMenuCode("DJ005");
                            m5.setMenuName("系统参数");
                            m5.setImageName("xitongcanshu.png");
                            m5.setMenuView("sys_PigGrading");
                            m5.setRemark("各种系统参数的定义功能");
                            menuEntityArrayList.add(m5);
                            break;
                        case "DJ006":
                            new MenuEntity();
                            MenuEntity m6 = new MenuEntity();
                            m6.setMenuCode("DJ006");
                            m6.setMenuName("定级入库查询");
                            m6.setImageName("baitiaoruku.png");
                            m6.setMenuView("sys_CarcassInWarehouseList");
                            m6.setRemark("宰后羊胴体的入库查询功能");
                            menuEntityArrayList.add(m6);
                            break;
                        case "DJ007":
                            new MenuEntity();
                            MenuEntity m7 = new MenuEntity();
                            m7.setMenuCode("DJ007");
                            m7.setMenuName("羊胴体出库");
                            m7.setImageName("baitiaochuku.png");
                            m7.setMenuView("sys_CarcassOutWarehouse");
                            m7.setRemark("宰后羊胴体的出库功能");
                            menuEntityArrayList.add(m7);
                    }
                }

                int pageCount = 1;
                ArrayList<GridPane> gridPanes = new ArrayList();

                for(int i = 0; i < pageCount; ++i) {
                    GridPane gridPane = new GridPane() {
                        protected void layoutChildren() {
                            super.layoutChildren();
                        }
                    };
                    gridPane.setAlignment(Pos.CENTER);
                    gridPanes.add(gridPane);
                }

                for(int i = 0; i < this.columnIndex * this.rowIndex * pageCount; ++i) {
                    Region node = null;
                    if (i < menuEntityArrayList.size()) {
                        MenuEntity curMenuEntity = (MenuEntity)menuEntityArrayList.get(i);
                        MenuButton menuButton = new MenuButton(curMenuEntity.getMenuName(), curMenuEntity.getRemark(), curMenuEntity.getImageName());
                        menuButton.setMenuViewName(curMenuEntity.getMenuView());
                        node = menuButton;
                    } else {
                        node = new Label();
                    }

                    VBox vBox = new VBox();
                    vBox.getChildren().add(node);
                    vBox.setPadding(new Insets(5.0));
                    VBox.setVgrow((Node)node, Priority.ALWAYS);
                    ((Region)node).prefWidthProperty().bind(vBox.widthProperty());
                    ((Region)node).prefHeightProperty().bind(vBox.heightProperty());
                    int paneIndex = (int)Math.ceil((double)(i / (this.rowIndex * this.columnIndex)));
                    ((GridPane)gridPanes.get(paneIndex)).add(vBox, i % this.columnIndex, (int)Math.ceil((double)((i - paneIndex * this.rowIndex * this.columnIndex) / this.columnIndex)));
                    GridPane.setHgrow(vBox, Priority.ALWAYS);
                    GridPane.setVgrow(vBox, Priority.ALWAYS);
                }

                this.pagination.setMaxPageIndicatorCount(pageCount);
                this.pagination.setPageFactory((pageIndex) -> {
                    return (Node)gridPanes.get(pageIndex);
                });
            }

            ApplicationContext.mainStage.show();
            if (Screen.getPrimary().getBounds().getWidth() == 1024.0) {
                ApplicationContext.mainStage.setMaximized(true);
            }
        }

    }
}
